package zy.zy7.maketVIP;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        File file = new File("E:\\cs.txt");
        ObjectOutputStream out = null;
        ObjectInputStream oin = null;

        Market market = null;
        try {
            oin = new ObjectInputStream(new FileInputStream(file));
            market = (Market) (oin.readObject());
            oin.close();
            System.out.println(market);
        } catch (EOFException e) {
            System.out.println("空文件");
            Market market1 = new Market("欢乐购", "南一环   ");
            try {
                out = new ObjectOutputStream(new FileOutputStream(file));
                out.writeObject(market1);
                oin = new ObjectInputStream(new FileInputStream(file));
                market = (Market) (oin.readObject());
                out.close();
                oin.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        People people = null;
        System.out.println("欢迎光临" + market.getName()+market.getAddress() + ",祝您购物愉快！");
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("1、登录 2、制卡 3、显示所有会员信息 4、退出");
            int a = sc.nextInt();
            switch (a) {
                case 1:
                    System.out.println("请输入登录账号和密码：");
                    people = market.login(sc.next(), sc.next());
                    if (people != null) {
                        while (true) {
                            System.out.println("1、增加积分 2、积分兑换 3、查询剩余积分 4、修改密码 5、退出");
                            int a1= sc.nextInt();
                            switch (a1) {
                                case 1:
                                    System.out.println("请输入要增加的积分：");
                                    market.addcredit(people,sc.nextInt());
                                    break;
                                case 2:
                                    System.out.println("请输入需要兑换的商品序号：1、纸巾（300积分） 2、血压计（1000积分） 3、iphone（10000积分）");
                                    market.convert(people,sc.nextInt());
                                    break;
                                case 3:
                                    market.lookcredit(people);
                                    break;
                                case 4:
                                    System.out.println("请输入原始密码：");
                                    String aa=sc.next();
                                    while (aa.equals(people.getPassword())==false){
                                        System.out.println("原密码错误，请重新输入");
                                        aa=sc.next();
                                    }
                                    System.out.println("请输入修改后密码：");
                                    String s = sc.next();
                                    market.changepassword(people,s);
                                    break;
                                case 5:
                                    break;
                            }
                        }

                    } else
                        break;
                case 2:
                    Calendar c = Calendar.getInstance();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd-HH:mm:ss");
                    String s=simpleDateFormat.format(c.getTime());
                    System.out.println("请输入新会员账号、姓名、密码、初始积分、电话：");
                    String vipNumber=sc.next();
                    String name=sc.next();
                    String password=sc.next();
                    int credit=sc.nextInt();
                    String phone=sc.next();
                    String startDate=s;
                    People people1=new People(vipNumber,name,password,credit,phone,startDate);
                    market.addMember(people1);
                    break;
                case 3:
                    market.show();
                    break;
                case 4:
                    break;

            }
            System.out.println("谢谢使用");
        }

    }
}
